
package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.AdminDogComFeedbackFromDto;
import com.dog.basics.bean.dto.api.common.ApiDogComFeedbackAddDto;
import com.dog.basics.bean.model.common.DogComFeedback;
import com.dog.basics.bean.vo.admin.common.AdminComFileInfosVo;
import com.dog.basics.bean.vo.admin.common.AdminDogComFeedbackVo;
import com.dog.basics.common.constant.ComFileInfoConstant;
import com.dog.basics.common.util.Json;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogComFeedbackMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.DogComFeedbackService;
import com.dog.basics.service.common.IDogComFileInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 意见反馈
 *
 * @author cy code generator
 * @date 2021-02-22 15:04:46
 */
@Service
@AllArgsConstructor
public class DogComFeedbackServiceImpl extends ServiceImpl<DogComFeedbackMapper, DogComFeedback> implements DogComFeedbackService {
  private final IDogComFileInfoService dogComFileInfoService;
    /**
     * @Description: 分页
     * @Param: status
     * @return: IPage
     * @Author: cy
     * @Date:
     */
    @Override
    public IPage<AdminDogComFeedbackVo> getAdminPageList(PageParam page, AdminDogComFeedbackFromDto obj) {
        // 查询数据信息
        IPage ipage = this.page(page, Wrappers.<DogComFeedback>lambdaQuery()
                .eq(ObjectUtil.isNotNull(obj.getFeedbackType()), DogComFeedback::getFeedbackType, obj.getFeedbackType())
                .like(StrUtil.isNotBlank(obj.getTelephone()), DogComFeedback::getTelephone, obj.getTelephone())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogComFeedback::getCreateTime));
        // 数据信息转型格式化
        List<DogComFeedback> records = page.getRecords();
        page.setRecords(records.stream().map(e -> {
            AdminDogComFeedbackVo toBean = BeanUtil.toBean(e, AdminDogComFeedbackVo.class);
            if (StrUtil.isNotBlank(e.getFeedbackImgArray())) {
                List<String> ts = null;
                try {
                    //ts = JSONUtil.toBean(e.getFeedbackImgArray(), ArrayList.class);
                    ts = Json.parseArray(e.getFeedbackImgArray(), String[].class);
                } catch (Exception ex) {
                    ts = CollUtil.newArrayList();
                }
                toBean.setFeedbackImgArray(ts);
            }
            List<AdminComFileInfosVo> fileInfos = dogComFileInfoService.getAdminComFileInfos(e.getId(), ComFileInfoConstant.FEEDBACK);
            toBean.setAdminComFileInfosVo(fileInfos);
            return toBean;
        }).collect(Collectors.toList()));
        return ipage;
    }

    /**
     * @Description: 查询详情
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    public AdminDogComFeedbackVo getAdminObjXq(Long id) {
        DogComFeedback dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        AdminDogComFeedbackVo newsVo = new AdminDogComFeedbackVo();
        BeanUtil.copyProperties(dogNews, newsVo);
        if (StrUtil.isNotBlank(dogNews.getFeedbackImgArray())) {
            List<String> ts = null;
            try {
                //ts = JSONUtil.toBean(e.getFeedbackImgArray(), ArrayList.class);
                ts = Json.parseArray(dogNews.getFeedbackImgArray(), String[].class);
            } catch (Exception ex) {
                ts = CollUtil.newArrayList();
            }
            newsVo.setFeedbackImgArray(ts);
        }
        List<AdminComFileInfosVo> fileInfos = dogComFileInfoService.getAdminComFileInfos(dogNews.getId(), ComFileInfoConstant.FEEDBACK);
        newsVo.setAdminComFileInfosVo(fileInfos);
        return newsVo;
    }


    /**
     * @Description: 增加APP
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveApiObj(ApiDogComFeedbackAddDto obj) {
        // 判断手机号码
        Validator.validateMobile(obj.getTelephone(), "common.mobile");
        // 执行添加数据
        DogComFeedback addFeedback = BeanUtil.toBean(obj, DogComFeedback.class);
        addFeedback.setCreateBy(SecurityUtils.getUser().getUserId());
        addFeedback.setCreateTime(DateUtil.currentSeconds());
        Validator.validateTrue(this.save(addFeedback), "common.save");
        if (CollUtil.isNotEmpty(obj.getFileIds())) {
            // 处理文件信息
            Boolean fileSuccess = dogComFileInfoService.editNotUsedComFileInfo(obj.getFileIds(), addFeedback.getId(), ComFileInfoConstant.FEEDBACK);
            Validator.validateTrue(fileSuccess, "file.error");
        }
        return Boolean.TRUE;
    }


    /**
     * @Description: 删除
     * @Param: ids
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        DogComFeedback dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        Validator.validateTrue(this.removeById(id), "common.del");
        List<Long> fileIds = CollUtil.newArrayList();
        // 处理文件信息
        Boolean fileSuccess = dogComFileInfoService.editDelUsedAddNotUsed(fileIds, dogNews.getId(), ComFileInfoConstant.FEEDBACK);
        Validator.validateTrue(fileSuccess, "file.error");
        return Boolean.TRUE;
    }

    /**
     * 意见处理
     *
     * @param id
     * @return
     */
    @Override
    public Boolean editFeedback(Long id) {
        // 判断意见是否存在
        DogComFeedback feedback = this.getById(id);
        Validator.validateNotNull(feedback, "common.noData");
        // 数据处理修改
        feedback.setFeedbackState(3);
        Validator.validateTrue(this.updateById(feedback), "common.edit");
        return Boolean.TRUE;
    }

}
